<?php

class Emms_Form_Savings_SavingsWithdrawal extends HTech_Form
{
    protected $_primary  = 'SavingsTransactionId';
    
    private $_client;
    private $_withdrawalLimit;
                    
    public function getValues()
    {
        $data = parent::getValues();
        unset($data['Client']);
        unset($data['Group']);
        unset($data['Location']);
        unset($data['Officer']);
        unset($data['WithdrawalLimit']);
        
        $accounts = new SavingsAccounts();
        $account = $accounts->getRecord($data['ClientId']);
        
        $data['Type'] = 'Cash withdrawal';
        $data['Date'] = $data['LastEditDate'];
        $data['Balance'] = $this->_client->d->Balance - $data['Amount'];
        $data['AccruedInterest'] = SavingsAccounts::calcAccruedInterest($account);
        $data['UserId'] = $data['LastEditUserId']; //print_r($data);exit;
        return $data;
    }
    
    private function _init()
    {
        try {
            $clients = new SavingsAccounts();
            $this->_client = $clients->getRecord((int) $this->_request->getParam('client'));
            $this->_withdrawalLimit = (double) $this->_client->d->Balance;
            
            $loans = new ActiveLoans();
            $select = $loans->select();
            $select->where('ClientId = ?', $this->_client->d->ClientId);
            $rows = $loans->fetchAll($select);
            foreach($rows as $loan) {
                $this->_withdrawalLimit -= $loan['ExpectedPaymentAmount'] + $loan['CurrentPenalties'];
            }
            //$this->_withdrawalLimit = max($this->_withdrawalLimit,0);
        } catch (Exception $e) {
            echo $e->getMessage(); exit;
        }
    }
    
    public function __construct() 
    {
        parent::__construct();  
        $this->_init();     
        
        $this->setName('SavingsWithdrawal');
        
        $this->setAttrib('title', $this->translate("Savings withdrawal form"));
        $this->setDescription($this->translate("Edit savings withdrawal information"));
          
        $savingsTransactionId = new Zend_Form_Element_Hidden('SavingsTransactionId');
        
        $clientId = new Zend_Form_Element_Hidden('ClientId');
        $clientId->setValue($this->_client->d->ClientId);
        
        $client = new Zend_Form_Element_Text('Client');
        $client->setLabel("Client")
            ->setRequired(true)
            ->setAttrib('readonly', 'true')
            ->setValue($this->_client->d->ClientFullName);
        
        $group = new Zend_Form_Element_Text('Group');
        $group->setLabel("Group")
            ->setAttrib('readonly', 'true')
            ->setValue($this->_client->d->GroupName);
        
        $location = new Zend_Form_Element_Text('Location');
        $location->setLabel("Location")
            ->setRequired(true)
            ->setAttrib('readonly', 'true')
            ->setValue($this->_client->d->LocationName);
        
        $officer = new Zend_Form_Element_Text('Officer');
        $officer->setLabel("Officer")
            ->setRequired(true)
            ->setAttrib('readonly', 'true')
            ->setValue($this->_client->d->OfficerFullName);
                       
        $balance = new Zend_Form_Element_Text('Balance');
        $balance->setLabel("Current balance")
            ->setAttrib('readonly', 'true')
            ->setValue(number_format($this->_client->d->Balance,2));
                       
        $limit = new Zend_Form_Element_Text('WithdrawalLimit');
        $limit->setLabel("Withdrawal limit")
            ->setAttrib('readonly', 'true')
            ->setValue(number_format($this->_withdrawalLimit,2));
                       
        $amount = new Zend_Form_Element_Text('Amount');
        $amount->setLabel("Withdrawal amount")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid amount")))
            ->addValidator('GreaterThan', true, array(
                'min' => 0,
                'inclusive' => false,
                'messages' => $this->translate("Invalid amount")))
            ->addValidator('LessThan', true, array(
                'max' => $this->_withdrawalLimit,
                'inclusive' => false,
                'messages' => $this->translate("Maximum withdrawal limit exceeded"))); 
                                
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 3); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));      
        
        $this->addElements(array($savingsTransactionId, $clientId,
            $client, $group, $location, $officer, $balance, $limit,
            $amount, $comments, $submit));
    }
}